package com.woniu.mealsService.controller;


import com.woniu.annotation.RequireRole;
import com.woniu.mealsService.service.StoreFileUploadService;
import com.woniu.utils.JwtUtil;
import com.woniu.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 商铺文件上传表 前端控制器
 * </p>
 *
 * @author jiang
 * @since 2024年12月09日
 */
@RestController
@RequestMapping("/storeFileUpload")
@Api(tags = "商铺文件上传")
public class StoreFileUploadController {
    @Autowired
    private StoreFileUploadService storeProductService;
    @ApiOperation(value = "菜品图片上传")
    @PostMapping("/upload")
    @RequireRole({"商家"})
    public Result upload(@RequestHeader String token, MultipartFile file) throws Exception {
        if (file==null){
            return Result.getResult().setMsg("无需上传图片");
        }
        String userId = JwtUtil.parseToken(token).get("userId").toString();
        String imgUrl=storeProductService.upload(file,userId);
        return Result.getResult(String.class).setMsg("上传成功").setData(imgUrl);
    }
    @ApiOperation(value = "菜品图片更改")
    @ApiImplicitParam(name = "fileUploadId",value = "更改的菜品id")
    @PutMapping("/updateFile")
    @RequireRole({"商家"})
    public Result updateFile(@RequestHeader String token, String fileUploadId, MultipartFile file) throws Exception {
        System.out.println(file);
        if (file==null){
            return Result.getResult().setMsg("无需上传图片");
        }
        String userId = JwtUtil.parseToken(token).get("userId").toString();
        storeProductService.updateFile(fileUploadId,file,userId);
        return Result.getResult().setMsg("上传成功");
    }
}

